import { onMounted, reactive, toRefs } from "vue";
import { getOption } from "@/api/index.js";

const isString = (val) =>
  Object.prototype.toString.call(val) === "[object String]";
export function useEnumSelect(props) {
  const { params } = props;

  const data = reactive({
    options: {},
    loading: false,
  });

  const loadData = () => {
    data.loading = true;
    data.options = {};

    let _params = params;
    if (isString(_params)) {
      _params = [_params];
    }

    return getOption({ enums: _params.join(',') }).then(
      (res) => {
        const { data: _data } = res;
        data.loading = false;
        data.options = _data || {};
        return _data;
      },
      (err) => {
        data.loading = false;
        data.options = {};
        return Promise.reject(err);
      }
    );
  };

  onMounted(() => {
    loadData();
  });
  return toRefs(data);
}
